package cn.com.cennavi.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.TreeMap;

/**
 * 滴滴数据与标准答案对比分析
 * 
 * @author Thinkpad
 *
 */
public class StandardDifFromDiDiLane {
//	private static String didihhmm0 = "0040";
	private static String didihhmm0 = "1504";
	private static String standardhhmm0 = "0913";
	private static SimpleDateFormat hf = new SimpleDateFormat("HHmm");
//	private static SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmm");
	private static String didiFile = "D:\\data\\分车道校验\\20160831.log";
	private static String statardFile = "D:\\data\\分车道校验\\标准答案.log";

	public static void main(String[] args) throws ParseException, IOException {
		// TODO Auto-generated method stub
		// 读取标准答案
		
		TreeMap<Integer, HashMap<String, StandardDifFromDiDiLaneData>> dataMap = getStandardLaneData(statardFile);
		// printdataMap(dataMap);
		getError(dataMap, didiFile);

	}

	/**
	 * 获取误差率
	 * 
	 * @param dataMap
	 * @param didiFile2
	 * @throws ParseException
	 * @throws IOException
	 */
	private static void getError(TreeMap<Integer, HashMap<String, StandardDifFromDiDiLaneData>> dataMap, String didiFile2) throws ParseException, IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(new File(didiFile2)));
		String line = "";
		HashSet<String> nodeDir = new HashSet<String>();
		while ((line = br.readLine()) != null) {
			LaneData data = LaneData.parseLine(line);
			String nodeId = data.roadId;
			int flag = 0;
			int timeDif = getMinDifFromStart(didihhmm0, data.timeStamp);
			if (!data.bef_straight_linkLength_speed_Arr.contains("-1")) {
				flag = 0;
				StandardDifFromDiDiLaneData data1 = new StandardDifFromDiDiLaneData(data.timeStamp,nodeId, timeDif, flag, data.bef_straight_linkLength_speed_Arr);
				HashMap<String, StandardDifFromDiDiLaneData> map1 = dataMap.get(timeDif);
				if (map1 != null) {
					StandardDifFromDiDiLaneData standardDifFromDiDiLaneData = map1.get(nodeId + "_" + flag);
					if (standardDifFromDiDiLaneData != null) {
						getError(data1, standardDifFromDiDiLaneData);
					}
				}

			}
			if (!data.bef_bent_linkLength_speed_Arr.contains("-1")) {
				flag = 1;
				StandardDifFromDiDiLaneData data1 = new StandardDifFromDiDiLaneData(data.timeStamp,nodeId, timeDif, flag, data.bef_bent_linkLength_speed_Arr);
				HashMap<String, StandardDifFromDiDiLaneData> map1 = dataMap.get(timeDif);
				if (map1 != null) {
					StandardDifFromDiDiLaneData standardDifFromDiDiLaneData = map1.get(nodeId + "_" + flag);
					if (standardDifFromDiDiLaneData != null) {
						getError(data1, standardDifFromDiDiLaneData);
					}
				}
			}
		}
	}

	/**
	 * jisuanwucha
	 * 
	 * @param data1
	 * @param standardDifFromDiDiLaneData
	 */
	private static void getError(StandardDifFromDiDiLaneData data1, StandardDifFromDiDiLaneData data2) {
		// TODO Auto-generated method stub
//		if((data1.nodeId + "," + data1.dirFlag + "," +data1.time + "," +data2.time + "," +data1.timeBuf).equals("546491,1,1841,0937,24"))
//			System.out.println(data1.nodeId + "," + data1.dirFlag + "," +data1.time + "," +data2.time + "," +data1.timeBuf);
		TreeMap<Integer, Integer> ss1 = data1.disSpeedMap;
		TreeMap<Integer, Integer> ss2 = data2.disSpeedMap;
		int size = (ss1.size() - ss2.size() > 0 ? ss2.size() : ss1.size());
		double sumError = 0;
		for (int i = 0; i < size; i++) {
			double error = Math.abs((double) (ss2.get(i) - ss1.get(i)) / ss2.get(i));
			sumError += error;
		}
		double erageError = sumError / size;
		String line=data1.nodeId + "," + data1.dirFlag + "," +data1.time + "," +data2.time + "," +data1.timeBuf + "," + erageError;
		System.out.println(line);
	}

	private static void printdataMap(TreeMap<Integer, HashMap<String, StandardDifFromDiDiLaneData>> dataMap) {
		// TODO Auto-generated method stub
		for (Map.Entry<Integer, HashMap<String, StandardDifFromDiDiLaneData>> entry1 : dataMap.entrySet()) {
			HashMap<String, StandardDifFromDiDiLaneData> dataMap1 = entry1.getValue();
			for (Map.Entry<String, StandardDifFromDiDiLaneData> entry : dataMap1.entrySet()) {
				System.out.println(entry.getKey() + "," + entry.getValue().disSpeedMap.keySet());
			}
		}
	}

	/**
	 * key:与开始时间的时间间隔分钟, value:key:<nodeId value:StandardDifFromDiDiLaneData>
	 * 
	 * @param statardFile2
	 * @return
	 * @throws IOException
	 * @throws ParseException
	 */
	private static TreeMap<Integer, HashMap<String, StandardDifFromDiDiLaneData>> getStandardLaneData(String statardFile2) throws IOException, ParseException {
		// TODO Auto-generated method stub
		HashSet<String> dadaaa = new HashSet<String>();
		TreeMap<Integer, HashMap<String, StandardDifFromDiDiLaneData>> returnMap = new TreeMap<Integer, HashMap<String, StandardDifFromDiDiLaneData>>();
		BufferedReader br = new BufferedReader(new FileReader(new File(statardFile2)));
		String line = "";
		HashSet<String> nodeDir = new HashSet<String>();
		while ((line = br.readLine()) != null) {
			LaneData data = LaneData.parseLine(line);
			String nodeId = data.roadId;

			int flag = 0;
			int timeDif = getMinDifFromStart(standardhhmm0, data.timeStamp);
			if (!data.bef_straight_linkLength_speed_Arr.contains("-1")) {
				flag = 0;
				dadaaa.add(nodeId + "," + flag);
				StandardDifFromDiDiLaneData data1 = new StandardDifFromDiDiLaneData(data.timeStamp,nodeId, timeDif, flag, data.bef_straight_linkLength_speed_Arr);
				// 如果之前包含了，就不再保存了
				if (nodeDir.contains(nodeId + "_" + flag)) {
//					System.out.println(nodeId);
					continue;
				} else {
					nodeDir.add(nodeId + "_" + flag);
				}

				HashMap<String, StandardDifFromDiDiLaneData> dataMap = returnMap.get(timeDif);
				if (dataMap == null) {
					dataMap = new HashMap<String, StandardDifFromDiDiLaneData>();
					dataMap.put(nodeId + "_" + flag, data1);
					returnMap.put(timeDif, dataMap);
				} else {

					// 如果前一分钟输出了，后一分钟就不输出了
					if (!dataMap.containsKey(nodeId + "_" + flag))
						dataMap.put(nodeId + "_" + flag, data1);

				}
			}
			if (!data.bef_bent_linkLength_speed_Arr.contains("-1")) {
				flag = 1;
				dadaaa.add(nodeId + "," + flag);
				StandardDifFromDiDiLaneData data1 = new StandardDifFromDiDiLaneData(data.timeStamp,nodeId, timeDif, flag, data.bef_bent_linkLength_speed_Arr);
				// 如果之前包含了，就不再保存了
				if (nodeDir.contains(nodeId + "_" + flag)) {
//					System.out.println(nodeId);
					continue;
				} else {
					nodeDir.add(nodeId + "_" + flag);
				}

				HashMap<String, StandardDifFromDiDiLaneData> dataMap = returnMap.get(timeDif);
				if (dataMap == null) {
					dataMap = new HashMap<String, StandardDifFromDiDiLaneData>();
					dataMap.put(nodeId + "_" + flag, data1);
					returnMap.put(timeDif, dataMap);
				} else {
					if (!dataMap.containsKey(nodeId + "_" + flag))
						dataMap.put(nodeId + "_" + flag, data1);
				}
			}
		}
//		System.out.println("dadaaa:" + dadaaa.size());
		HashSet<String>dd=new HashSet<String>();
		for(String sa:dadaaa){
			String ssss=sa.split(",")[0];
			if(ssss.equals("658359")){
//				System.out.println("dadaaa:" + dadaaa.size());
			}
			if(dd.contains(ssss)){
//				System.out.println(ssss);
			}else{
				dd.add(ssss);
			}
		}
		return returnMap;
	}

	/**
	 * 获取两个时间间隔，返回分钟
	 * 
	 * @param startTime
	 * @param nowTime
	 * @return
	 * @throws ParseException
	 */
	public static int getMinDifFromStart1(String startTime, String nowTime) throws ParseException {
		String startHHmm=startTime.substring(8, 12);
		String nowHHmm=startTime.substring(8, 12);
		Date dateDiDi = hf.parse(startHHmm);
		Date dateDiDi1 = hf.parse(nowHHmm);
		int min = (int) ((dateDiDi1.getTime() - dateDiDi.getTime()) / 1000 / 60);
		if (min < 0) {
			min += 1440;
		}
		// System.out.println(min);
		return min;
	}
	/**
	 * 获取两个时间间隔，返回分钟
	 * 
	 * @param startTime
	 * @param nowTime
	 * @return
	 * @throws ParseException
	 */
	public static int getMinDifFromStart(String startTime, String nowTime) throws ParseException {
		if(nowTime.length()==12){
			String startHHmm=startTime;
			String nowHHmm=nowTime.substring(8, 12);
			Date dateDiDi = hf.parse(startHHmm);
			Date dateDiDi1 = hf.parse(nowHHmm);
			int min = (int) ((dateDiDi1.getTime() - dateDiDi.getTime()) / 1000 / 60);
			if (min < 0) {
				min += 1440;
			}
			// System.out.println(min);
			return min;
		}else{
			Date dateDiDi = hf.parse(startTime);
			Date dateDiDi1 = hf.parse(nowTime);
			int min = (int) ((dateDiDi1.getTime() - dateDiDi.getTime()) / 1000 / 60);
			if (min < 0) {
				min += 1440;
			}
			// System.out.println(min);
			return min;
		}
		
	}
}
